
function Fact_Base(a_id, a_factid)
{
	Object_Fact.apply(this, [ a_id ]);
	// fact id
	this.fact_id = a_factid;
	// name
	this.name = null;
	// 绘图信息
	this.drawResources = new Draw_Normal();
	// 优先级 用于鼠标选中的优先级
	this.view_priority = null;
	// 图片资源路径
	this.imagesrc = null;
	// 状态
	this.state = null;
	// 位置信息
	this.position = new Position_Normal();
	// 上下文
	this.context = null;
	//
	this.fact_info = null;
	// 元素的操作状态 0为true 1为false
	// 0-可否onclick 1-可否onmove 2-可否ondoublclick ...等等 拓展
	this.op_state = new Array(0, 0, 0);
	// 元素的显示状态
	this.view_state = new Array(0, 0, 0);
	// 
	this.viewResources = new Array();
	// 绘图内容容器
	this.views = new Can_Fact();
	// 界面的静态配置类
	this.view = null;
	// run的开关
	this.onoff = true;
	// timer 用于画图的间隔
	this.timer = null;

}
extend_usemid(Fact_Base, Object_Fact);

/**
 * 初始化静态信息
 */
Fact_Base.prototype.init = function()
{
	this.fact_info.init();
}


/**
 * 可点击可视的对象数组
 * op是指明click move等之类 不写就是全指
 * order是add或者pop 添加或者忽略 0是添加 1是忽略
 */
Fact_Base.prototype.updateViewResource = function(a_fact, order, op)
{
	var _isArray = a_fact instanceof Array;
	if (_isArray)
	{
		for ( var i in a_fact)
		{
			var _position = -1;
			for ( var k in this.viewResources)
			{
				if (this.viewResources[k].fact_id === a_fact[i].fact_id)
				{
					_position = k;
					break;
				}
			}

			if (_position !== -1)
			{
				if (op === undefined || op === null)
				{
					for ( var j in this.viewResources[_position].op_state)
					{
						this.viewResources[_position].op_state[j] = order;
					}
				}
				else
				{
					this.viewResources[_position].op_state[op] = order;
				}
			}
			else
			{
				for ( var j in a_fact[i].op_state)
				{
					a_fact[i].op_state[j] = order;
				}
				this.viewResources.push(a_fact[i]);
			}
		}
	}
	else
	{
		var _position = -1;
		for ( var k in this.viewResources)
		{
			if (this.viewResources[k].fact_id === a_fact.fact_id)
			{
				_position = k;
				break;
			}
		}

		if (_position !== -1)
		{
			if (op === undefined || op === null)
			{
				for ( var j in this.viewResources[_position].op_state)
				{
					this.viewResources[_position].op_state[j] = order;
				}
			}
			else
			{
				this.viewResources[_position].op_state[op] = order;
			}
		}
		else
		{
			a_fact.op_state[op] = order;
			this.viewResources.push(a_fact);
		}
	}

	quickSort(this.viewResources, "view_priority", "<");

}


/**
 * 点击事件捕获
 */
Fact_Base.prototype.findSelect = function(evt)
{
	alert(evt.offsetX + " " + evt.offsetY);
	var _selectObj = getClickObject(this.view.facts, "datas", evt.offsetX,
			evt.offsetY);
	if (_selectObj !== null)
	{
		return _selectObj;
	}
}

/**
 * 更新对象属性 更新a_obj中字段名为a_targetname的对象 匹配该对象中matchkey字段的值是不是matchvalue
 * 是则将匹配到的那一个对象修改名为a_fieldkey的对象的值变为a_fieldvalue a_fieldkey和a_fieldvalue可以是数组
 * ps:当a_targetname对象不是数组或者长度为1时候,方法忽略matchkey和matchvalue
 */
Fact_Base.prototype.updateAttr = function(a_obj, a_targetname, matchkey,
		matchvalue, a_fieldkey, a_fieldvalue)
{
	var _isTargetArray = a_obj[a_targetname] instanceof Array;
	var _isFieldArray = a_fieldkey instanceof Array;
	var _target = a_obj[a_targetname];
	// 需要被修改的对象
	var _result = null;

	if (_isTargetArray)
	{
		var _length = _target.length;
		if (_length > 1)
		{
			for ( var i = 0; i < a_target.length; i++)
			{
				if (_target[i][matchkey] === matchvalue)
				{
					_result = _target[i];
					break;
				}
			}
		}
		else if (_length === 1)
		{
			_result = _target[0];
		}
		else
		{
			return "error:no this attr";
		}
	}
	else
	{
		_result = _target;
	}

	// 执行修改
	if (_isFieldArray)
	{
		for ( var k = 0; k < a_fieldnames.length; k++)
		{
			_result[a_fieldkey[k]] = a_fieldvalue[k];
		}
	}
	else
	{
		_result[a_fieldkey] = a_fieldvalue;
	}
}

/**
 * 对象每个属性的赋值(复制),从协议中
 */
Fact_Base.prototype.findAttr = function(obj, targetname, fieldname, fieldvalue)
{
	var _result = null;
	// 要搜寻的那个
	var _targetAttr = obj[targetname];
	var _isArray = _targetAttr instanceof Array;
	// 是数组
	if (_isArray)
	{
		for ( var i = 0; i < _targetAttr.length; i++)
		{
			if (_targetAttr[i][fieldname] === fieldvalue)
			{
				_result = _targetAttr[i];
				break;
			}
		}
	}
	else
	{
		if (_targetAttr[fieldname] = fieldvalue)
		{
			_result = _targetAttr;
		}
	}
	return _result;
}
/**
 * 把obj的key属性赋值成value
 */
Fact_Base.prototype.intern = function(obj, key)
{
	obj.cvs[key] = this;
	obj.cvs.onclick = function(evt)
	{
		this[key].onClick(evt, this)
	};

	obj.websocket[key] = this;
	obj.websocket.onmessage = function(evt)
	{
		this[key].onRecv(evt, this)
	};
}

/**
 * 对象每个属性的赋值(复制),从协议中
 */
Fact_Base.prototype.updateFromPro = function(a_pro, a_order)
{
	var _obj = null;
	for ( var attr in a_pro)
	{
		if (typeof (this[attr]) !== "function" && a_pro[attr] !== undefined
				&& a_pro[attr] !== null)
		{
			// 该属性是数组
			if (a_pro[attr] instanceof Array)
			{
				for ( var each in a_pro[attr])
				{
					this[attr][each].parseFromPro(a_pro[attr][each], a_order);
					this[attr].push(_obj);
				}
			}
			else if (typeof (a_pro[attr]) === "object"
					&& a_pro[attr].constructor === Object)
			{
				_obj = eval("new " + a_pro[attr]["fact_type"] + "();");
				_obj.parseFromPro(a_pro[attr], "attr4pro");
				this[attr] = _obj;
			}
			else
			{
				this[attr] = a_pro[attr];
			}
		}
	}
}

Fact_Base.prototype.drawContent = function(order)
{
	var _attrs = order.split("-");
	// 属性名
	var _firstStr = _attrs[0];
	var _eachs = _firstStr.split(":");
	var _eachsStr = _eachs.join("\"][\"");
	var _target = eval("this[\"" + _eachsStr + "\"]");

	// 方法名 drawSelf or drawContent
	var _method_name = _attrs[1] === undefined || _attrs[1] === null ? "Self"
			: _attrs[1];
	// 递归参数数组
	// var _rest_arg = _attrs.splice(0, 2);
	_attrs.shift();
	_attrs.shift();
	// 转成可以递归的字符串样式
	var _rest_arg_str = _attrs.join("-");

	if (_target !== undefined && _target !== null)
	{
		if (_target instanceof Array)
		{
			for ( var each in _target)
			{
				// var _arg = _method_name === "Self" ? null : _rest_arg_str;
				// alert("this[\"" + _eachsStr + "\"][\"" + each + "\"]" +
				// ".draw"
				// + _method_name + "(\"" + _rest_arg_str + "\")");
				eval("this[\"" + _eachsStr + "\"][\"" + each + "\"]" + ".draw"
						+ _method_name + "(\"" + _rest_arg_str + "\")");
			}
			this.updateViewResource(_target, 0);
		}
		else if (_target.datas !== undefined && _target.datas !== null
				&& _target.datas instanceof Array)
		{
			for ( var eachData in _target["datas"])
			{
//				 alert("this[\"" + _eachsStr + "\"][\"datas\"][\"" + eachData
//				 + "\"]" + ".draw" + _method_name + "(\""
//				 + _rest_arg_str + "\")");
				eval("this[\"" + _eachsStr + "\"][\"datas\"][\"" + eachData
						+ "\"]" + ".draw" + _method_name + "(\""
						+ _rest_arg_str + "\")");
				
				if (_method_name === "Self")
				{
					this.updateViewResource(_target["datas"][eachData], 0);
				}
				
			}
//			this.updateViewResource(_target["datas"], 0);
		}
		else
		{
			eval("this[\"" + _eachsStr + "\"]" + ".draw" + _method_name + "(\""
					+ _rest_arg_str + "\")");
			if (_method_name === "Self")
			{
				this.updateViewResource(_target, 0);
			}
		}
	}
	else
	{
		alert("'_attr_name' is null or undefined");
	}
}

Fact_Base.prototype.cleanContent = function(order)
{
	var _attrs = order.split("-");
	// 属性名
	var _firstStr = _attrs[0];
	var _eachs = _firstStr.split(":");
	var _eachsStr = _eachs.join("\"][\"");
	var _target = eval("this[\"" + _eachsStr + "\"]");

	// 方法名 drawSelf or drawContent
	var _method_name = _attrs[1] === undefined || _attrs[1] === null ? "Self"
			: _attrs[1];
	// 递归参数数组
	// var _rest_arg = _attrs.splice(0, 2);
	_attrs.shift();
	_attrs.shift();
	// 转成可以递归的字符串样式
	var _rest_arg_str = _attrs.join("-");

	if (_target !== undefined && _target !== null)
	{
		if (_target instanceof Array)
		{
			for ( var each in _target)
			{
				// var _arg = _method_name === "Self" ? null : _rest_arg_str;
				// alert("this[\"" + _eachsStr + "\"][\"" + each + "\"]" +
				// ".draw"
				// + _method_name + "(\"" + _rest_arg_str + "\")");
				eval("this[\"" + _eachsStr + "\"][\"" + each + "\"]" + ".clean"
						+ _method_name + "(\"" + _rest_arg_str + "\")");
			}
			this.updateViewResource(_target, 1);
			alert("array: " + _target[0]["hero_name"] + " clean");
		}
		else if (_target.datas !== undefined && _target.datas !== null
				&& _target.datas instanceof Array)
		{
			for ( var eachData in _target["datas"])
			{
				// alert("this[\"" + _eachsStr + "\"][\"datas\"][\"" + eachData
				// + "\"]" + ".draw" + _method_name + "(\""
				// + _rest_arg_str + "\")");
				eval("this[\"" + _eachsStr + "\"][\"datas\"][\"" + eachData
						+ "\"]" + ".clean" + _method_name + "(\""
						+ _rest_arg_str + "\")");
			}
			this.updateViewResource(_target["datas"], 1);
			alert("can clean:" + _target["datas"].length + " " + _target["datas"][0]["hero_name"] + " clean");
		}
		else
		{
			eval("this[\"" + _eachsStr + "\"]" + ".clean" + _method_name + "(\""
					+ _rest_arg_str + "\")");
			if (_method_name === "Self")
			{
				this.updateViewResource(_target, 1);
				alert(_target["fact_id"] + " clean");
			}
		}
	}
	else
	{
		alert("'_attr_name' is null or undefined");
	}
}

/**
 * 绘图方法
 */
Fact_Base.prototype.draw = function()
{
	for(var i = 0; i < this.views.datas.length; i++)
	{
		this.views.datas[i].drawSelf();
	}
}

Fact_Base.prototype.drawView = function()
{
	for(var i = 0; i < this.view.facts.length; i++)
	{
		this.view.facts[i].drawView();
	}
}

/**
 * 画出绘图资源
 */
Fact_Base.prototype.drawSelf = function(imagesrc)
{
	var _time = 1;
	var _this = this;

//	this.timer = setInterval(function()
//	{
//		_this.draw(imagesrc);
//	}, _time);
	// 可画则画
	if(this.view_state[0] === 0)
	{
		if (imagesrc === undefined || imagesrc === null || imagesrc === "")
		{
			this.drawResources.drawSelf(this.imagesrc, this.position);
		}
		else
		{
			this.drawResources.drawSelf(this[imagesrc], this.position);
		}
	}
}


/**
 * 清除自己
 */
Fact_Base.prototype.cleanSelf = function()
{
//	window.clearInterval(this.timer);
	this.drawResources.cleanSelf(this.position);
}

/**
 * 关掉timer循环
 */
Fact_Base.prototype.stopInterval = function()
{
	window.clearInterval(this.timer);
}

/**
 * 关掉onoff开关
 */
Fact_Base.prototype.turn = function(action)
{
	if(action === "on")
	{
		this.onoff = true;
	}
	else if(action === "off")
	{
		this.onoff = false;
	}
}




/**
 * onRecv
 */
Fact_Base.prototype.onRecv = function(msg, attach)
{

}

// //////////////////////////////////
// //////////////////////////////////
// //////////////////////////////////

function Fact_Game(a_id, a_factid)
{
	Fact_Base.apply(this, arguments);
}
extend_usemid(Fact_Game, Fact_Base);

/**
 * 
 */
function Fact_Scene(a_id, a_factid)
{
	Fact_Base.apply(this, arguments);
	// 用户
	this.users = new Can_Fact(10);
	this.background = new UI_Normal();
}
extend_usemid(Fact_Scene, Fact_Base);

/**
 * 退出
 */
Fact_Scene.prototype.quit = function(user_id, user_name)
{
	alert("quit " + user_id + " uname:" + user_name);
	
	this.users.popByField("id", user_id);
}

Fact_Scene.prototype.join = function(aUser)
{
	alert("a new user join");
	this.users.add(aUser);
}

Fact_Scene.prototype.onRecv = function(msg)
{
	var parsed = JSON.parse(msg.data);
	alert("on Fact_Scene Recv:" + parsed.name + "||" + msg.data);
	switch (parsed.pro_id)
	{
	// 别人进入场景
	case '551':
		var _user = tranObject(parsed["user"]);
		this.join(_user);
		break;
	// 有人断线退出场景
	case '503':
		this.quit(parsed.user_id, parsed.user_name);
		break;
	case 'error':
		alert("error");
		break;
	default:
		break;
	}
}

/**
 * 
 */
function Fact_UI(a_id, a_factid)
{
	Fact_Base.apply(this, arguments);
}
extend_usemid(Fact_UI, Fact_Base);

/**
 * 
 */
function Fact_Info(a_id, a_factid)
{
	Fact_Base.apply(this, arguments);
}
extend_usemid(Fact_Info, Fact_Base);
